﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.Context;

namespace PHSRAG.Insight21.Agreements.Reports
{
    /// <remarks>Class for generating Arra report</remarks>
    public class sqlArraReport : sqlReport
    {
        private const string SearchCacheKey = "ArraSearchCacheKey_7357C24C-19D3-4d5e-8807-5133DFDB6C6F";
        private const string SortKey = "ArraSortKey_D4442682-6248-4db4-9C34-202150D730F2";
        private const string SortDirection = "ArraSortDirection_D3BD203C-93CD-4b99-BE14-1AA6E3158F03";  

        public sqlArraReport(string pageUrl) : base(pageUrl)
        {
            doCustomExcelReport = true;
        }
                
        public override ParameterValue[] setParameters(ref string rptName)
        {
            Hashtable hst = GetSearchFromCache();
            ArrayList reportParams = new ArrayList();
            string SortField = "";
            string SortDir = "";
            string FilterCriteria = "";

            ParameterValue paramValue = new ParameterValue();
            paramValue.Name = "ReportPeriodId";
            paramValue.Value = hst["ReportPeriodID"].ToString();
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "fwkDomainUserId";
            paramValue.Value = ((UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey)).DomainUserId.ToString();
            reportParams.Add(paramValue);

            if (hst["PSFundNo"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "PSFundNo";
                paramValue.Value = hst["PSFundNo"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = "PSFund# : " + hst["PSFundNo"].ToString() + " | ";
            }

            if (hst["AwardNo"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "SponsorAwardNo";
                paramValue.Value = hst["AwardNo"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "Award# : " + hst["AwardNo"].ToString() + " | ";
            }

            if (hst["PILastName"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "PILastName";
                paramValue.Value = hst["PILastName"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "PILastName : " + hst["PILastName"].ToString() + " | ";
            }

            if (hst["PIFirstName"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "PIFirstName";
                paramValue.Value = hst["PIFirstName"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "PIFirstName : " + hst["PIFirstName"].ToString() + " | ";
            }

            if (hst["Sponsor"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "Sponsor";
                paramValue.Value = hst["Sponsor"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "Sponsor : " + hst["Sponsor"].ToString() + " | ";
            }

            if (hst["ProposalNo"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "@ProposalNo";
                paramValue.Value = hst["ProposalNo"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "Proposal# : " + hst["ProposalNo"].ToString() + " | ";
            }

            if (hst["DAFirstName"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "DAFirstName";
                paramValue.Value = hst["DAFirstName"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "DAFirstName : " + hst["DAFirstName"].ToString() + " | ";
            }

            if (hst["DALastName"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "DALastName";
                paramValue.Value = hst["DALastName"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "DALastName : " + hst["DALastName"].ToString() + " | ";
            }

            if (hst["DocumentID"].ToString().Trim() != "")
            {
                paramValue = new ParameterValue();
                paramValue.Name = "DocumentID";
                paramValue.Value = hst["DocumentID"].ToString();
                reportParams.Add(paramValue);
                FilterCriteria = FilterCriteria + "DocumentID : " + hst["DocumentID"].ToString() + " | ";
            }

            Hashtable h = pageCachePolicy.GetCachedObject(Keys.StimulusFundingPageKey) as Hashtable;
            SortField = (h == null || h[SortKey] == null) ? "" : h[SortKey].ToString();
            SortDir = (h == null || h[SortDirection] == null) ? "" : h[SortDirection].ToString();
            SortField = SortField + " " + SortDir;

            paramValue = new ParameterValue();
            paramValue.Name = "OrderBy";
            paramValue.Value = SortField;
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "PageSize";
            paramValue.Value = "0";
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "PageStartIndex";
            paramValue.Value = "0";
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "PageEndIndex";
            paramValue.Value = "1";
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "reporttitle";
            paramValue.Value = hst["ReportPeriodName"].ToString();
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "username";
            paramValue.Value = ((UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey)).FirstName.ToString() + " " + ((UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey)).LastName.ToString();
            reportParams.Add(paramValue);

            paramValue = new ParameterValue();
            paramValue.Name = "filtercriteria";
            paramValue.Value = FilterCriteria;
            reportParams.Add(paramValue);

            ParameterValue[] oInputPrms = (ParameterValue[])reportParams.ToArray(typeof(ParameterValue));

            return oInputPrms;
        }

        /// <summary>
        /// Method to retrieve the search data from the session
        /// </summary>
        /// <returns></returns>
        private Hashtable GetSearchFromCache()
        {
            Hashtable h = pageCachePolicy.GetCachedObject(Keys.StimulusFundingPageKey) as Hashtable;
            if (h == null || h[SearchCacheKey] == null)
            {
                Hashtable SearchDictionary = new Hashtable();
                return SearchDictionary;
            }
            else
            {
                return (Hashtable)h[SearchCacheKey];
            }
        }
    }
}
